﻿using System;
using System.Collections.Generic;
using System.Text;
using DevCore.Utility;

namespace DevCore.Service.Db
{

#region 数据库字段默认值设置。
    /// <summary>
    /// 数据库默认值设置
    /// </summary>
    public class DbColumnDefaultValueSetter
    {
        private String _value = String.Empty;
        public String Value
        { 
            get
                {
                    return _value;
                }
            set
            {
                _value = value;
            }
        }
        
        public DbColumnDefaultValueSetter(String val)
        {
            _value = val;
        }

        public override string  ToString()
        {
            return _value != null ? _value : String.Empty;
        }

        /// <summary>
        /// 根据字段类型返回当前字段的默认值
        /// </summary>
        /// <param name="typeName">类型名称</param>
        /// <returns></returns>
        public static DbColumnDefaultValueSetter SetDefaultByType(String typeName)
        {
            String retValue = "";
            if (StringHelper.ObjectIsEmpty(typeName)) return null;

            typeName = typeName.ToLower();
            //如果是以下类型的话，那么是默认值是0
            if (typeName == "bigint" || typeName == "binary" || typeName == "bit" || typeName == "decimal" || typeName == "float"
                || typeName == "int" || typeName == "money" || typeName == "numeric" || typeName == "real" || typeName == "smallint"
                || typeName == "smallmoney" || typeName == "timestamp" || typeName == "tinyint" || typeName == "varbinary")
            {
                retValue = "0";
            }
            else if (typeName == "char" || typeName == "image" || typeName == "nchar" || typeName == "ntext" || typeName == "nvarchar"
                    || typeName == "sql_variant" || typeName == "text" || typeName == "varchar")
                {
                    retValue = "''";
                }
                else if (typeName == "datetime" || typeName == "smalldatetime")
                {
                    retValue = "GETDATE()";
                }
                else if (typeName == "uniqueidentifier")
                {
                    retValue = "NEWID()";
                }

             return new DbColumnDefaultValueSetter(retValue);;

        }





    }

#endregion
    /// <summary>
    /// 数据库操作相关
    /// </summary>
    public class DbService
    {
        #region 表相关
        /// <summary>
        /// 创建默认的表
        /// </summary>
        /// <param name="tbName"></param>
        /// <returns></returns>
        public static int CreateDefaultTable(String tbName)
        {
            int retValue = -1;

            String strSql = "CREATE TABLE [{0}](id int NOT NULL IDENTITY (1, 1)) ON [PRIMARY]";
            DbDataService.ExecuteSql(String.Format(strSql, tbName));

            return retValue;
        }
        #endregion

        #region 字段相关

        /// <summary>
        /// 向表中增加一个字段
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="columnName">列名</param>
        /// <param name="type"></param>
        /// <param name="isNull"></param>
        /// <param name="defaultValue"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static int AddColumn(String tableName, String columnName, String type, bool  isNull, DbColumnDefaultValueSetter defaultValue, String remark)
        {







            return 0;
        }

        

        #endregion

        #region 存储过程相关

        #endregion


        #region 视图相关

        #endregion
    }
}
